Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Functions → Lambda Functions

Python Functions

Lambda Functions

Python's lambda functions, also known as anonymous functions, provide a concise way to create small, single-expression functions without the need for a formal `def` statement. They're particularly useful for short, simple operations that don't require multiple statements or complex logic. Think of them as \"throw-away\" functions often used where a full function definition would be unnecessarily verbose.

Syntax

The basic syntax is:
Basic syntax lambda arguments: expression
✦ `lambda`: The keyword that indicates a lambda function. ✦ `arguments`: A comma-separated list of input arguments, similar to a regular function's parameters. ✦ `expression`: A single expression that is evaluated and returned. This cannot contain multiple statements or complex control flow (like `if-else` blocks within the expression itself; those would require a regular function).

Example 1: Simple addition

Let's create a lambda function to add two numbers:
Simple lambda function example in python add = lambda x, y: x + y # Assigns the lambda function to the variable 'add' result = add(5, 3) # Call the lambda function print(result)

Output

8

This is equivalent to a regular function:
Function example def add_regular(x, y): return x + y result = add_regular(5,3) print(result)

Output

8

The lambda version is more compact, but for anything beyond a single expression, the `def` version is preferred for readability.

Example 2: Squaring a number

Let's create a lambda function to square a number
square = lambda x: x * x result = square(7) print(result)

Output

49

Example 3: Using lambda functions with `map()`

Lambda functions shine when used with higher-order functions like `map()`, `filter()`, and `sorted()`. `map()` applies a function to each item in an iterable (like a list).
Using lambda functions with `map()` numbers = [1, 2, 3, 4, 5] squared_numbers = list(map(lambda x: x * x, numbers)) #Applies the lambda function to each element in numbers print(squared_numbers)

Output

[1, 4, 9, 16, 25]
Without a lambda function, you'd need a separate `def` statement.

Example 4: Using lambda functions with `filter()`

`filter()` creates an iterator from elements of an iterable for which a function returns `True`.
Using lambda functions with `filter()` numbers = [1, 2, 3, 4, 5, 6] even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers)

Output

[2, 4, 6]

Example 5: Lambda with multiple arguments and a slightly more complex expression

Lambda with multiple arguments calculate_area = lambda length, width: length * width if length > 0 and width > 0 else 0 # conditional expression area1 = calculate_area(5, 10) area2 = calculate_area(-2, 5) # handles invalid input print(area1) print(area2)

Output

50 0

Limitations

Single expression: Lambda functions can only contain a single expression. They cannot have multiple statements or complex control flow structures like `if-else` blocks (though conditional expressions are allowed as shown above). Readability: For complex operations, lambda functions can become less readable than regular functions. Prioritize readability; use `def` for more complex logic. No docstrings: Lambda functions don't support docstrings, making them less suitable for documenting complex logic. In summary, lambda functions are a powerful tool for creating concise, anonymous functions in Python, particularly useful for short operations and when used in conjunction with higher-order functions. However, it's crucial to use them judiciously, prioritizing readability and maintainability. For anything beyond a simple, single expression, a regular function is often a better choice.

Tutorials